package com.tang.generate.utils


import com.tang.framework.config.SpringApplication
import com.tang.generate.model.GenVO
import com.tang.generate.service.IGenFieldTypeService
import org.apache.commons.lang3.StringUtils


object GenUtils {


    private val importMap: Map<String, String> = hashMapOf(
        "decimal" to "import java.math.BigDecimal;",
        "date" to "import java.time.LocalDate;",
        "time" to "import java.time.LocalTime;",
        "dateTime" to "import java.time.LocalDateTime;"
    );

    private val javaTypeMap: Map<String, String> = hashMapOf(
        "varchar" to "String",
        "date" to "LocalDate",
        "time" to "LocalTime",
        "datetime" to "LocalDateTime",
        "longtext" to "String",
        "text" to "String",
        "bigint" to "Long",
        "tinyint" to "Boolean",
        "int" to "Integer",
        "decimal" to "BigDecimal",
        "char" to "String"
    );


    fun processImportList(genVO: GenVO) {
        val genFieldTypeService: IGenFieldTypeService =
            SpringApplication.getBean<IGenFieldTypeService>(IGenFieldTypeService::class.java)
        genVO.fieldList?.let { fieldList ->
            fieldList.forEach { item ->
                with(item) {
                    if (this.import!!) {
                        genVO.importList?.run {
                            add("import com.alibaba.excel.annotation.ExcelProperty;")
                        }
                    }
                    if (this.tableFieldType!!.contains("date")) {
                        genVO.importList?.run {
                            add("import com.fasterxml.jackson.annotation.JsonFormat;")
                        }
                    }
                    val impoort_: String? = genFieldTypeService.getImportByName(this.fieldType);
                    if (StringUtils.isNotBlank(impoort_)) {
                        genVO.importList?.add(impoort_!!)
                    }
//                    tableFieldType?.let {
//                        if (importMap.contains(it)) {
//                            importMap[it]?.let { import ->
//                                genVO.importList?.run {
//                                    add(import)
//                                }
//                            }
//                        }
//                    }
                }
            }
        }
    }


    fun getClazzName(tableName: String): String {
        val clazzName = StringBuilder()
        val strings = tableName.split("_".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        for (item in strings) {
            val chars = item.toCharArray()
            if (chars[0] in 'a'..'z') {
                chars[0] = Character.toUpperCase(chars[0])
            }
            clazzName.append(String(chars))
        }
        return clazzName.toString()
    }


    fun getClassName(tableName: String): String? {
        val className = StringBuilder()
        val strings = tableName.split("_".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        className.append(strings[0])
        for (i in strings.indices) {
            if (i > 0) {
                val chars = strings[i].toCharArray()
                if (chars[0] in 'a'..'z') {
                    chars[0] = Character.toUpperCase(chars[0])
                }
                className.append(String(chars))
            }
        }
        return className.toString()
    }

    fun getTag(tableName: String): String? {
        val Tag = StringBuilder()
        val strings = tableName.split("_".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        for (i in strings.indices) {
            Tag.append(strings[i])
            if (i < strings.size - 1) {
                Tag.append("/")
            }
        }
        return Tag.toString()
    }

    fun getPackageName(tableName: String): String? {
        return tableName.replace("_", ".")
    }

    fun getJavaType(dataType: String): String? {
        return javaTypeMap[dataType];

    }
}
